package com.lsh.payment.core.handler.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.payment.api.model.basevo.BaseResponse;
import com.lsh.payment.api.model.basevo.ExceptionStatus;
import com.lsh.payment.api.model.mispurse.MisRefundResponse;
import com.lsh.payment.api.model.refund.RefundResponse;
import com.lsh.payment.core.handler.AbstractRefundChannelHandler;
import com.lsh.payment.core.model.payenum.RefundStatus;
import com.lsh.payment.core.model.payment.PayDeal;
import com.lsh.payment.core.model.refund.PayRefund;
import com.lsh.payment.core.service.mispurse.IMisPurseRefundService;
import com.lsh.payment.core.service.refund.IRefundOrderStatusService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Project Name: lsh-payment
 *
 * @author peter
 * @date 18/6/1
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
@Slf4j
public class MisPurseRefundHandler extends AbstractRefundChannelHandler {

    @Autowired
    private IMisPurseRefundService misPurseRefundService;

    @Override
    public RefundResponse refund(PayRefund payRefund, PayDeal payDeal) {
        String logPrefix = "【mis退款】";
        RefundResponse refundResponse = new RefundResponse();
        MisRefundResponse response = misPurseRefundService.refund(payRefund, payDeal);
        if (response.getRet() == 0) {
            log.info("【mis退款结果】 {}", JSON.toJSONString(response));
            IRefundOrderStatusService refundOrderStatusService = this.getRefundOrderStatusService(RefundStatus.REFUND_ING);
            payRefund.setChannelRefundId(response.getTransactionId());
            if (refundOrderStatusService.modifyStatus(payRefund, true)) {
                JSONObject content = new JSONObject();
                content.put("payment_refund_no", payRefund.getPaymentRefundNo());
                refundResponse.setContent(content);
            }

            refundResponse.setRet(Integer.parseInt(ExceptionStatus.SUCCESS.getCode()));
            refundResponse.setMsg(ExceptionStatus.SUCCESS.getMessage());
        } else {
            log.info("{} 返回失败", logPrefix);
//            log.info("sub_code:{},sub_msg:{}", response.getSubCode(), response.getSubMsg());
            String ext = payRefund.getExt();
            JSONObject extJson;
            if (StringUtils.isNotBlank(ext)) {
                extJson = JSON.parseObject(ext);
            } else {
                extJson = new JSONObject();
            }
            extJson.put("err_code", response.getRet());
            extJson.put("err_code_des", response.getMsg());
            payRefund.setExt(extJson.toJSONString());
            this.getRefundOrderStatusService(RefundStatus.REFUND_EXCEPTION).modifyStatus(payRefund, false);

            refundResponse.setRet(2001005);
            refundResponse.setMsg("退款申请失败," + response.getRet() + "," + response.getMsg());
        }

        return refundResponse;
    }


    @Override
    public BaseResponse refundQuery(PayRefund payRefund, PayDeal payDeal) {
        String logPrefix = "【mis退款查询】";
        BaseResponse baseResponse = new BaseResponse();

        if (payRefund.getRefundStatus() == RefundStatus.REFUND_ING.getValue()) {
            MisRefundResponse response = misPurseRefundService.refundQuery(payRefund, payDeal);
            log.info("{} 结果 {}", logPrefix, JSON.toJSONString(response));
            if (response.getRet() == 0) {
                    if (this.getRefundOrderStatusService(RefundStatus.REFUND_SUCCESS).modifyStatus(payRefund, true)) {
                        baseResponse.setRet(0);
                        baseResponse.setMsg("mis退款成功");
                    }

                    return baseResponse;
            } else {
                String ext = payRefund.getExt();
                JSONObject extJson;
                if (StringUtils.isNotBlank(ext)) {
                    extJson = JSON.parseObject(ext);
                } else {
                    extJson = new JSONObject();
                }
                extJson.put("err_code", response.getRet());
                extJson.put("err_code_des", response.getMsg());
                payRefund.setExt(extJson.toJSONString());
                if (this.getRefundOrderStatusService(RefundStatus.REFUND_EXCEPTION).modifyStatus(payRefund, true)) {
                    baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2006003.getCode()));
                    baseResponse.setMsg("退款申请成功，查询退款结果FAIL,请联系技术人员");
                }
            }
        } else if (payRefund.getRefundStatus() == RefundStatus.REFUND_SUCCESS.getValue()) {
            baseResponse.setRet(0);
            baseResponse.setMsg("mis退款成功");
        } else if (payRefund.getRefundStatus() == RefundStatus.REFUND_NEW.getValue()) {
            baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2000000.getCode()));
            baseResponse.setMsg("mis退款进行中");
        } else if (payRefund.getRefundStatus() == RefundStatus.REFUND_APPLY.getValue()) {
            baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2000000.getCode()));
            baseResponse.setMsg("mis退款进行中");
        } else if (payRefund.getRefundStatus() == RefundStatus.REFUND_CLOSE.getValue()) {
            baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2006001.getCode()));
            baseResponse.setMsg(ExceptionStatus.E2006001.getMessage());
        } else {
            baseResponse.setRet(Integer.valueOf(ExceptionStatus.E2006002.getCode()));
            baseResponse.setMsg("mis退款失败");
        }

        return baseResponse;
    }


}
